/*
 *  AMediaDesc.cpp
 *  libavid
 *
 *  Created by Peter on 25.02.12.
 *  Copyright 2012 __MyCompanyName__. All rights reserved.
 *
 */

#include "AMediaDesc.h"
#include "AStream.h"
#include "AObjDoc.h"

A_IMPLEMENT_CLASS( 'MDES', AMediaDesc, AObject );

void AMediaDesc::readFromStream( AStream& str, const AObjDoc& objd )
{
	UInt8 tagMark, version;
	str.readPod( tagMark );
	str.readPod( version );
	TR_ASSERT( tagMark == 0x02 );
	TR_ASSERT( version == 0x03 );
	
	str.readPod( _mobKind );
	str.readPod( _locator ); // TODO: Resolve to MSML pointer via AObjDoc
	
	AObject* obj = objd.resolveRef< AObject >( _locator );
	if( obj )
		TR_TRACE( "AMediaDesc::_locator is of type " << obj->getClassName() );
	
	_intermediate = str.readBool();
	str.readPod( _physicalMedia ); // TODO: Resolve to pointer, too
	
	// Checking for extension TagMark, if we don't have the extension,
	// we're done
	if( str.peek() != 0x01 ) {
		_haveUid = false;
		return;
	}
	
	_haveUid = true;
	
	str.readPod( tagMark );
	str.readPod( version );
	TR_CHECK( tagMark == 0x01 );
	TR_ASSERT( version == 0x01 );
	str.readPod( tagMark );
	TR_ASSERT( tagMark == 65 );
	
	UInt32 bytesLen;
	str.readPod( bytesLen );
	TR_ASSERT( bytesLen == 16 );
	_acfUid.readFromStream( str );
}

#define TRACE_FIELD( x ) "\t" #x ": " << x << std::endl
#define TRACE_CHAR_FIELD( x ) "\t" #x ": " << static_cast< int >( x ) << std::endl

void AMediaDesc::dump( std::ostream& str ) const
{
	str << "AMediaDesc {" << std::endl;
	
	str << TRACE_CHAR_FIELD( _mobKind );
	str << TRACE_FIELD( _locator );
	str << TRACE_FIELD( _intermediate );
	str << TRACE_FIELD( _physicalMedia );
	
	if( _haveUid )
		str << TRACE_FIELD( _acfUid );
	
	str << "}" << std::endl;
}